import { reqGoodsInfo,reqAddOrUpdateShopCart } from "@/api"

const state = {
    goodInfo: {}
}
const mutations = {
    GETGOODINFO(state, goodInfo){
        state.goodInfo = goodInfo
    }
}
const actions = {
    async getGoodInfo({commit},skuid){
        let rst = await reqGoodsInfo(skuid)
        if(rst.code==200){
            commit('GETGOODINFO', rst.data)
        }        
    },
    async addShopCart({commit},{skuId,skuNum}){
        let result  = await reqAddOrUpdateShopCart(skuId,skuNum);
        //加入购物车成功
        if(result.code==200){
            //代表当前函数addShopCart返回的promise状态是成功
            return 'ok';
        }else{
            return Promise.reject(new Error('faile'));
        }
        //一个函数执行，默认返回的是undefined，代表promise成功，
        //函数执行默认返回的是undefined，永远是成功，什么时候undefined不起作用了
    }
}
const getters = {
    categoryView(state){
        return state.goodInfo.categoryView||{}
    },
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList||{}
    },
    skuInfo(state){
        return state.goodInfo.skuInfo||{}
    },
}

export default{
    state,
    mutations,
    actions,
    getters,
}